package com.wanlian.router.ui.manage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.smart.android.ui.BaseFragment;
import com.smart.android.ui.app.IntentExtra;
import com.smart.android.widget.EditTextWithClear;
import com.wanlian.router.R;
import com.wanlian.router.bean.RouterInfo;
import com.wanlian.router.bean.WifiSetting;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/9/8.
 * Email: fvaryu@163.com
 */
public class IPDynamicSettingFragment extends BaseFragment implements TextWatcher {


    @BindView(R.id.rg_dns)
    RadioGroup rg_connect;
    @BindView(R.id.rb_auto)
    RadioButton rb_auto;
    @BindView(R.id.rb_self)
    RadioButton rb_self;
    @BindView(R.id.et_dns1)
    EditTextWithClear et_dns1;
    @BindView(R.id.et_dns2)
    EditTextWithClear et_dns2;

    @BindView(R.id.tv_need)
    TextView tvNeed;
    @BindView(R.id.tv_next)
    TextView next;
    @BindView(R.id.tv_need_2)
    TextView tv_need_2;

    public static IPDynamicSettingFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(IntentExtra.EXTRA_TYPE, type);
        IPDynamicSettingFragment fragment = new IPDynamicSettingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int layout() {
        return R.layout.fragment_ip_dynamic_setting;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        et_dns1.addTextChangedListener(this);

        rg_connect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_self) {
                    tvNeed.setVisibility(View.VISIBLE);
                    tv_need_2.setVisibility(View.VISIBLE);
                    et_dns1.setEnabled(true);
                    et_dns2.setEnabled(true);

                    et_dns1.setHint("请输入DNS");
                    et_dns1.setText(null);
                    et_dns2.setText(null);
                    et_dns2.setHint("请输入DNS");

                    next.setEnabled(false);
                } else {
                    tvNeed.setVisibility(View.GONE);
                    tv_need_2.setVisibility(View.GONE);
                    et_dns1.setEnabled(false);
                    et_dns2.setEnabled(false);

                    et_dns1.setHint("系统自动分配DNS");
                    et_dns1.setText(null);
                    et_dns2.setText(null);
                    et_dns2.setHint("系统自动分配DNS");
                    next.setEnabled(true);

                }
            }
        });
    }

    @Override
    protected void initData(View view) {
        super.initData(view);

        NetWorkingClient.getInstance().queryCurrentRouterInfo(getActivity(), new INetCallBack<RouterInfo>() {
            @Override
            public void onFinish(ResponseData response, @Nullable RouterInfo info) {
                if (response.isSuccess()) {
                    if (info != null) {
                        if (info.getDeviceConfig() == null) {
                            return;
                        }
                        WifiSetting wifiSetting = info.getDeviceConfig();
                        rg_connect.check(wifiSetting.getDynamicType() == 0 ?  R.id.rb_auto : R.id.rb_self);

                        et_dns1.setText(wifiSetting.getOneDNS());
                        et_dns2.setText(wifiSetting.getTwoDNS());

                    }
                }
            }
        });
    }

    private boolean v(String string) {
        if (TextUtils.isEmpty(string)) {
            return false;
        }
        String regex = "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";
        return string.matches(regex);
    }

    @OnClick(R.id.tv_next)
    void next() {

        if ((et_dns2.getText() != null && !TextUtils.isEmpty(et_dns2.getText().toString())) && !v(et_dns2.getText().toString())) {
            showToast("请输入有效DNS");
            return;
        }
        WifiSetting wifiSetting = new WifiSetting();
        wifiSetting.setSord("dhcp");
        wifiSetting.setDynamicType(rg_connect.getCheckedRadioButtonId() == R.id.rb_auto ? 0 : 1);
        if (rg_connect.getCheckedRadioButtonId() != R.id.rb_auto) {
            wifiSetting.setOneDNS(et_dns1.getText().toString());
            wifiSetting.setTwoDNS(et_dns2.getText().toString());
        }

        NetWorkingClient.getInstance().settingWifi(getActivity(), false, false, wifiSetting, new INetCallBack() {
            @Override
            public void onFinish(ResponseData response, @Nullable Object o) {
                if (response.isSuccess()) {

                    showToast("设置成功");
                } else {
                    showToast(response.getMessage());
                }
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {


        next.setEnabled(
                et_dns1.getText() != null && v(et_dns1.getText().toString())
        );

    }
}
